#ifndef BOOST_STATIC_PTR_HPP_INCLUDED
#define BOOST_STATIC_PTR_HPP_INCLUDED

//
//  static_ptr.hpp
//
//  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
//  Copyright (c) 2001, 2002 Peter Dimov
//
//  Permission to copy, use, modify, sell and distribute this software
//  is granted provided this copyright notice appears in all copies.
//  This software is provided "as is" without express or implied
//  warranty, and with no claim as to its suitability for any purpose.
//
//  See http://www.boost.org/libs/smart_ptr/static_ptr.htm for documentation.
//

#include <boost/config.hpp>   // for broken compiler workarounds

#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
#include <boost/detail/static_ptr_nmt.hpp>
#else

#include <boost/assert.hpp>
#include <boost/checked_delete.hpp>
#include <boost/throw_exception.hpp>

#include <memory>             // for std::auto_ptr
#include <algorithm>          // for std::swap
#include <functional>         // for std::less
#include <typeinfo>           // for std::bad_cast

#ifdef BOOST_MSVC  // moved here to work around VC++ compiler crash
# pragma warning(push)
# pragma warning(disable:4284) // odd return type for operator->
#endif

namespace boost
{

namespace detail
{

template<typename T> struct static_ptr_traits
{
    typedef T & reference;
};

template<> struct static_ptr_traits<void>
{
    typedef void reference;
};

#if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)

template<> struct static_ptr_traits<void const>
{
    typedef void reference;
};

#endif

} // namespace detail


//
//  static_ptr
//

template<typename T> class static_ptr
{
private:
    typedef static_ptr<T> this_type;

public:

    typedef T element_type;
    typedef T value_type;

    static_ptr(): px(0)
    {
    }

    template<typename Y>
    static_ptr(Y * p): px(p)
    {
    }

	//  generated copy constructor, assignment, destructor are fine
    template<typename Y>
    explicit static_ptr(static_ptr<Y> const & r): px(r.px) // may throw
    {
    }

	template<typename Y>
	static_ptr(shared_ptr<Y> const & r):px(r.get())
	{
	}

	template<typename Y>
	static_ptr(weak_ptr<Y> const & r):px(r.get())
	{
	}

    template<typename Y>
    static_ptr(static_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
    {
        if (px == 0)
        {
            boost::throw_exception(std::bad_cast());
        }
    }

#if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)

    template<typename Y>
    static_ptr & operator=(static_ptr<Y> const & r) // never throws
    {
        px = r.px;
        return *this;
    }

#endif

    void reset()
    {
		px=0;
    }

    template<typename Y> void reset(Y * p) // Y must be complete
    {
		p=px;
    }

    typename detail::static_ptr_traits<T>::reference operator* () const // never throws
    {
        BOOST_ASSERT(px != 0);
        return *px;
    }

    T * operator-> () const // never throws
    {
        BOOST_ASSERT(px != 0);
        return px;
    }
    
    T * get() const // never throws
    {
        return px;
    }

    // implicit conversion to "bool"

    typedef T * (this_type::*unspecified_bool_type)() const;

    operator unspecified_bool_type() const // never throws
    {
        return px == 0? 0: &this_type::get;
    }

    bool operator! () const // never throws
    {
        return px == 0;
    }

    void swap(static_ptr<T> & other) // never throws
    {
        std::swap(px, other.px);
        pn.swap(other.pn);
    }

private:
    T * px;                     // contained pointer

};  // static_ptr

template<typename T, typename U> inline bool operator==(static_ptr<T> const & a, static_ptr<U> const & b)
{
    return a.get() == b.get();
}

template<typename T, typename U> inline bool operator!=(static_ptr<T> const & a, static_ptr<U> const & b)
{
    return a.get() != b.get();
}

template<typename T> inline bool operator<(static_ptr<T> const & a, static_ptr<T> const & b)
{
    return std::less<T*>()(a.get(), b.get());
}

// get_pointer() enables boost::mem_fn to recognize static_ptr
template<typename T> inline T * get_pointer(static_ptr<T> const & p)
{
    return p.get();
}


template<typename T, typename U>
static_ptr<T> static_dynamic_cast( static_ptr<U> ptr)
{
	return static_ptr<T>( dynamic_cast<T*>(ptr.get()) );
}

} // namespace boost

#ifdef BOOST_MSVC
# pragma warning(pop)
#endif    

#endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)

#endif  // #ifndef BOOST_static_PTR_HPP_INCLUDED
